<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us" lang="en-us">
<head>
  <link href="//gmpg.org/xfn/11" rel="profile">
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta name="generator" content="Hugo 0.69.0" />

  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>识别图片数字验证码 &middot; 我的博客</title>

  
  <link type="text/css" rel="stylesheet" href="/myhugo/css/print.css" media="print">
  <link type="text/css" rel="stylesheet" href="/myhugo/css/poole.css">
  <link type="text/css" rel="stylesheet" href="/myhugo/css/syntax.css">
  <link type="text/css" rel="stylesheet" href="/myhugo/css/hyde.css">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Abril+Fatface|PT+Sans:400,400i,700">


  
  <link rel="apple-touch-icon-precomposed" sizes="144x144" href="/apple-touch-icon-144-precomposed.png">
  <link rel="shortcut icon" href="/favicon.png">

  
  
</head>

  <body class="theme-base-0d ">
  <aside class="sidebar">
  <div class="container sidebar-sticky">
    <div class="sidebar-about">
      <a href="/myhugo/"><h1>我的博客</h1></a>
      <p class="lead">
       小白的逆袭之旅 
      </p>
    </div>

    <nav>
      <ul class="sidebar-nav">
        <li><a href="/myhugo/">Home</a> </li>
        
      </ul>
    </nav>

    <p>&copy; 2020. All rights reserved. </p>
  </div>
</aside>

    <main class="content container">
    <div class="post">
  <h1>识别图片数字验证码</h1>
  <time datetime=2020-04-26T22:36:53&#43;0800 class="post-date">Sun, Apr 26, 2020</time>
  <p>攻略：https://v3u.cn/a_id_134</p>
<p>京东云：http://open.jd.com</p>
<pre><code>import requests
import base64
import urllib

#获取密钥
res = requests.get(&quot;https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&amp;client_id=RQSCANnqoAs9Bk1cTnOWSema&amp;client_secret=G318YSye2P5vxv5g7xBfkwO9NOjBFd72&quot;)  #这个url是由https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials，加client_id=，加应用API Key，加client_secret=，加应用的Secret Key

token = res.json()['access_token']
# 定义头部信息
headers = {
    'Content-Type': 'application/x-www-from/urlencoded'
}
url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic?access_token=' + token

# 读取图片
my_img = open('1.png', 'rb')
tem_img = my_img.read()
my_img.close()
# 进行base64编码
temp_data = {'image': base64.b64encode(tem_img)}
# 对图片地址进行urlencode操作
temp_data = urllib.parse.urlencode(temp_data)

# 请求视图接口
res = requests.post(url=url, data=temp_data, headers=headers)
code = res.json()['words_result']
print(code)
str1=''
for i in code:
    value=i['words']
    str1+=value
print(str1)</code></pre>
</div>


    </main>

    
  </body>
</html>
